package com.googlecode.gwtbb.client.only.widget.form;

import com.googlecode.gwtbb.client.only.remote.ClientSession;
import com.googlecode.gwtbb.client.only.widget.form.entry.FormEntry;
import com.googlecode.gwtbb.client.only.widget.form.entry.LabeledTextBoxFormEntry;
import com.googlecode.gwtbb.client.shared.entity.picture.Picture;
import com.googlecode.gwtbb.client.shared.validation.Failure;
import com.googlecode.gwtbb.client.shared.validation.FieldId;
import com.googlecode.gwtbb.client.shared.validation.GwtbbValidator;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Abraham Grief
 * @version $Id$
 * @since Jan 19, 2009
 */
public class CaptionForm extends FormWidget {
	public CaptionForm(ClientSession session) {
		super(session);
		init();
	}

	public String getCaption() {
		return getValue(Picture.CAPTION, 0);
	}

	public List<FormEntry> getFormEntries() {
		List<FormEntry> retVal = new ArrayList<FormEntry>(1);
		LabeledTextBoxFormEntry captionEntry = createLabeledTextBoxFormEntry(Picture.CAPTION, 0, null, 0, 0, cmss.fieldLabelCaption());
		retVal.add(captionEntry);
		return retVal;
	}

	public String getProcessName() {
		return "captionEntry";
	}

	public String getProcessSuccess() {
		return null;
	}

	public String getProcessTitle() {
		return cmss.captionEntryProcessTitle();
	}

	public void setCaption(String caption) {
		setValue(Picture.CAPTION, 0, caption);
	}

	public Map<FieldId, Failure> validate(Map<FieldId, Failure> failures, int index) {
		String caption = GwtbbValidator.max(failures, FieldId.create(Picture.CAPTION, 0), getCaption(), Picture.CAPTION_MAX);
		setValue(Picture.CAPTION, 0, caption);
		return failures;
	}

	protected void doAction() {
		// the caption form is immediately succesful because we don't need to do anything besides store the caption,
		// which is done during validate
		handleSuccess(null);
	}
}
